function resizeContent(){
	var widthKanbanGroups;
	var contentSize = $("#content").width();
	var bodyWidth = $("body").width();
	var bodyHeight = $(window).height();

	if(contentSize < 1000){
		$("#content").width(1000);		
	}else if(bodyWidth >= 1000){
		$("#content").width("100%");
	}

	if(bodyHeight < 500){
		$("#box_content").height(385);
	}else{
		var topHeight = 126;
		$("#box_content").height(bodyHeight - topHeight);
	}
}

$(window).resize(function() {
	resizeContent();
});

$(document).ready(function() {	
	resizeContent();

	$("#menu_filter").click(function(){
		if($("#toolsbar .filter").css("display") == "none"){
			$("#toolsbar").slideUp('fast',function(){
				$(".tools_left > div",this).hide();
				$(".filter",this).show();

				$('#findBacklog').keyup(function(){
					var valField = new RegExp($(this).val(),"i");
					$("#backlog ul li").each(function(){
						if($(".title",this).text().search(valField) >= 0)
							$(this).css("display","block");
						else
							$(this).css("display","none");
					});
				});

				$(this).slideDown('slow');
			});
		}
	});

	$("#toolsbar .cancel").click(function(){
		$("#toolsbar").slideUp('fast',function(){
			$(".tools_left > div",this).hide();
			$(".title",this).show();
			$("#tools_open",this).show();

			$(this).slideDown('slow');
		});
	});

	$("#new_project").click(function(){		
		$.ajax({
			url: SITE+"app/Views/V_Project.php?f=newProject",
			success: function(data){
				var obj =  JSON.decode(data);
				resultNewProject(obj);
			}
		});
	});

	$("#archive_project").click(function(){		
		pagination(0,"&archive=true");
		$("#tools_open").slideUp("fast",function(){
			$("#toolsbar .title h3").html("Projetos Arquivados");
			$("#tools_archive").slideDown("fast");
		});		
	});	

	$("#open_project").click(function(){		
		pagination(0,"");
		$("#tools_archive").slideUp("fast",function(){
			$("#toolsbar .title h3").html("Projetos em Andamento");
			$("#tools_open").slideDown("fast");
		});		
	});	


	$("#formSearch").validate({
		submitHandler: function(form) {
			var findProject = $("#findProject").val();		
			addHash("s",findProject);
			pagination(getHash("p"),"&s="+findProject);

			return false;
		}
	});	

	
});

function resultNewProject(obj){	
	var html = "";

	html = obj[1];
	openBox("overlay", html);
	
	if(obj[0]){
		$("#formProject").validate({
			submitHandler: function(form) {
				var query = $(form).serialize();
				
				$.ajax({
					url: SITE+"app/Views/V_Project.php?f=saveProject&"+query,
					async: false,
					success: function(data){
						var obj =  JSON.decode(data);

						if(obj[0]){
							pagination(getHash("p"),"");
						}

						resultMensage(obj, obj[0]);
					}
				});

				return false;
			}
		});	
	
		$(".date-pick").dateinput();
		$("#dateBegin").data("dateinput").change(function() {
			$("#dateEndEstimated").data("dateinput").setMin(this.getValue(), true);
		});
	}else{
		eval(obj[2]);
	}

}

function pagination(val, args){
	$("#box_content").html('<center><img src="'+SITE+'files/images/icons/loading.gif"></center>');

	if(args == undefined)
		args = "";

	$.ajax({
		url: SITE+"app/Views/V_Project.php?f=getProjects&page="+val+""+args,
		success: function(data){
			var obj =  JSON.decode(data);
			if(obj[0]){
				$("#box_content").html(obj[1]);
			}else{
				resultMensage(obj, true);
				$("#box_content").html("");
			}	
		}
	});
}

function showModal(){}
function closeModal(){}

function editProject(id){
	$.ajax({
		url: SITE+"app/Views/V_Project.php?f=editProject&id="+id,
		success: function(data){
			var obj =  JSON.decode(data);
			resultNewProject(obj);
		}
	});
}

function viewProject(id){
	$.ajax({
		url: SITE+"app/Views/V_Project.php?f=viewProject&id="+id,
		success: function(data){
			var obj =  JSON.decode(data);
			if(obj[0]){
				openBox("overlay",obj[1]);
			}else{
				resultMensage(obj, true);
			}
		}
	});
}

function deleteProject(id){
	openBox("modalDelete");

	$("#modalDelete #confirm").click(function(){
		$.ajax({
			url: SITE+"app/Views/V_Project.php?f=deleteProject&id="+id,
			success: function(data){
				var obj =  JSON.decode(data);
				if(obj[0]){
					pagination(getHash("p"), "");
				}

				resultMensage(obj, true);
			}
		});
	});
}
 

function archiveProject(id){
	$.ajax({
		url: SITE+"app/Views/V_Project.php?f=archiveProject&id="+id,
		success: function(data){
			var obj =  JSON.decode(data);
			if(obj[0]){
				pagination(getHash("p"),"");
			}

			resultMensage(obj, true);
		}
	});
}